<?php

class SiteController extends Controller {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to access 'index' and 'view' actions.
                'actions' => array('index', 'register', 'login', 'logout'),
                'users' => array('*'),
            ),
//            array('deny', // allow all users to access 'index' and 'view' actions.
//                'actions' => array('register', 'login'),
//                'users' => array('?'),
//            ),
            array('allow', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function init() {
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/common.css');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/common.js", CClientScript::POS_HEAD);
        $cs->registerCoreScript('jquery');
        $cs->registerCoreScript('jquery.ui');
        $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/jquery.placeholder.js", CClientScript::POS_HEAD);
    }

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionIndex() {
//        print_r(Yii::app()->user->info['email']);
//        echo Yii::app()->params['params'];
        print_r(Yii::app());
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/index.css');

        $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=**&wt=json&indent=true');
        $row = json_decode($row, true);
        $row_hot_week = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/select?q=create_time:2014-07-08 19:23:19&wt=json&indent=true&sort=sale_count+des');
        $row_hot_week = json_decode($row_hot_week, true);
//        print_r($row);
        $this->render('index', array('model' => $row['results'], 'row_hot_week' => $row_hot_week));
    }

    public function actionProduct() {
        $allcount = 0;
        $skuValueId = array();
        $skuValue = array();
        
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile(Yii::app()->baseUrl . '/css/food.css');
        $model = new ProductForm('addorder');
        $productSkuId = $_GET['id'];
        $row = @file_get_contents('http://' . Yii::app()->params['apiHostProduct'] . ':' . Yii::app()->params['apiPortProduct'] . '/ProductCenter/skuInfo?productSkuId=' . $productSkuId);
        $row = json_decode($row, true);

        $productSku = $row['productSku'];

        for ($i = 0; $i < count($productSku); $i++) {  
            $skuprice[$productSku[$i]['skuSelectValue']]['skuPrice'] = $productSku[$i]['skuPrice']; //js选择sku数组  价格
            $skuprice[$productSku[$i]['skuSelectValue']]['quantity'] = $productSku[$i]['quantity']; //js选择sku数组  库存
            
            $allcount += $productSku[$i]['quantity'];
            $skulist = $productSku[$i]['productSkuValue'];
            for ($k = 0; $k < count($skulist); $k++) {
                $skuValuelist[$i]['skuValueId'][] = $skulist[$k]['skuValueId'];
                $skuValuelist[$i]['skuValue'][] = $skulist[$k]['skuValue'];
            }
        }
        $skuprice = json_encode($skuprice);
        $skuprice = str_replace('[', '', $skuprice);
        $skuprice = str_replace(']', '', $skuprice);

        $product = $row['product'];
        $keywords = explode(',', $row['product']['keywords']);

        $api = new Api('product');

        $params = array(
            'GetProductCommentByIdReq' => array(
                'sellerId' => '0',
                'productId' => $productSkuId
            )
        );
//        print_r($row);
        $commont_row = $api->apiclient('/LdAccountServer/comment/getProductCommentById', $params);
        $this->render('product', array('product' => $product, 'keywords' => $keywords, 'commont' => $commont_row['GetProductCommentByIdRsp']['productComments'], 'productSku' => $productSku, 'skuprice' => $skuprice, 'model' => $model,'skuValuelist'=>$skuValuelist,'allcount'=>$allcount));
    }

    public function actionError() {
        if ($error = Yii::app()->errorHandler->error) {
            if (Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

    public function actionLogin() {
        if (Yii::app()->user->isGuest) {
            $cs = Yii::app()->clientScript;
            $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');
            $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/zhbform.js", CClientScript::POS_HEAD);
            $model = new LoginForm('login');

            if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            if (isset($_POST['LoginForm'])) {
                $model->attributes = $_POST['LoginForm'];

                if ($model->validate() && $model->login())
                    $this->redirect(Yii::app()->homeUrl);
            }
            $this->layout = 'clearmain';
            $this->render('login', array('model' => $model));
        }else {
            header("location: " . Yii::app()->params['homeUrl']);
        }
    }

    public function actionRegister() {
        if (Yii::app()->user->isGuest) {
            $cs = Yii::app()->clientScript;
            $cs->registerCssFile(Yii::app()->baseUrl . '/css/login.css');
            $cs->registerScriptFile(Yii::app()->request->baseUrl . "/js/zhbform.js", CClientScript::POS_HEAD);

            $model = new LoginForm('register');

            if (isset($_POST['ajax']) && $_POST['ajax'] === 'reg-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            if (isset($_POST['LoginForm'])) {
                $model->attributes = $_POST['LoginForm'];

                if ($model->validate() && $model->register())
                    $this->redirect($this->createUrl('/'));
            }

            $this->layout = 'clearmain';
            $this->render('register', array('model' => $model));
        }else {
            header("location: " . Yii::app()->params['homeUrl']);
        }
    }

    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

}
